//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.cnhis.drgs.crypt;


import org.apache.commons.io.IOUtils;

import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.KeyGenerator;
import java.io.*;
import java.security.Key;
import java.security.SecureRandom;

public class Aes {
    private static String CHARSET = "utf-8";
    private Key key;

    public Aes() {
        this("jmjn");
    }

    public Aes(String key) {
        this.setKey(key);
    }

    public void setKey(String keyStr) {
        try {
            KeyGenerator _generator = KeyGenerator.getInstance("AES");
            SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
            secureRandom.setSeed(keyStr.getBytes());
            _generator.init(128, secureRandom);
            this.key = _generator.generateKey();
        } catch (Exception var4) {
            throw new RuntimeException(var4);
        }
    }

    public String encode(String str) {
        try {
            byte[] b = str.getBytes(CHARSET);
            byte[] b_ = this.encode(b);
            return Base64.encode(b_);
        } catch (Exception var4) {
            throw new RuntimeException(var4);
        }
    }

    public String decode(String str) {
        try {
            byte[] b = Base64.decode(str);
            byte[] b_ = this.decode(b);
            return new String(b_, CHARSET);
        } catch (Exception var4) {
            throw new RuntimeException(var4);
        }
    }

    public byte[] encode(byte[] b) {
        try {
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(1, this.key);
            byte[] byteFina = cipher.doFinal(b);
            return byteFina;
        } catch (Exception var5) {
            throw new RuntimeException(var5);
        }
    }

    public byte[] decode(byte[] b) {
        try {
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(2, this.key);
            byte[] byteFina = cipher.doFinal(b);
            return byteFina;
        } catch (Exception var5) {
            throw new RuntimeException(var5);
        }
    }

    public void encode(File src, File dest) {
        InputStream is = null;
        OutputStream out = null;
        CipherInputStream cis = null;

        try {
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(1, this.key);
            is = new FileInputStream(src);
            out = new FileOutputStream(dest);
            cis = new CipherInputStream(is, cipher);
            byte[] buffer = new byte[1024];

            int r;
            while((r = cis.read(buffer)) > 0) {
                out.write(buffer, 0, r);
            }
        } catch (Exception var12) {
            throw new RuntimeException(var12);
        } finally {
            IOUtils.closeQuietly(cis);
            IOUtils.closeQuietly(is);
            IOUtils.closeQuietly(out);
        }

    }

    public void decode(File src, File dest) {
        InputStream is = null;
        OutputStream out = null;
        CipherOutputStream cos = null;

        try {
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(2, this.key);
            is = new FileInputStream(src);
            out = new FileOutputStream(dest);
            cos = new CipherOutputStream(out, cipher);
            byte[] buffer = new byte[1024];

            int r;
            while((r = is.read(buffer)) >= 0) {
                cos.write(buffer, 0, r);
            }
        } catch (Exception var12) {
            throw new RuntimeException(var12);
        } finally {
            IOUtils.closeQuietly(cos);
            IOUtils.closeQuietly(out);
            IOUtils.closeQuietly(is);
        }

    }
}
